package bai8.com.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.rmi.RemoteException;

import bai8.com.command.Message;
import bai8.com.command.MessageFactory;

/**
 * @author nglehien Oct 21, 2012 - 8:44:26 PM
 */

public class ServerConnection extends Thread {

	private Socket socket;
	private ObjectInputStream objectInputStream;
	private ObjectOutputStream objectOutputStream;
	private boolean isCont = true;

	public ServerConnection(Socket socket) {
		this.socket = socket;
		start();
	}

	private void handle() throws IOException, ClassNotFoundException {
		objectInputStream = new ObjectInputStream(socket.getInputStream());
		objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
		while (isCont) {
			Message message = null;
			if ((message = receiveFromClient()) != null) {
				int command = message.getCommand();
				String data = message.getData();
				Message output;
				switch (command) {
				case MessageFactory.UPPER:
					data = data.toUpperCase();
					output = MessageFactory.createOutput(data);
					sendToClient(output);
					break;
				case MessageFactory.LOWER:
					data = data.toLowerCase();
					output = MessageFactory.createOutput(data);
					sendToClient(output);
					break;
				case MessageFactory.INSERT:
					int position = message.getPosition();
					String strInsert = message.getInsert();
					data = insert(position, data, strInsert);
					output = MessageFactory.createOutput(data);
					sendToClient(output);
					break;
				case MessageFactory.DELETE:
					String strDelete = message.getDelete();
					data = delete(data, strDelete);
					output = MessageFactory.createOutput(data);
					sendToClient(output);
					break;
				case MessageFactory.QUIT:
					isCont = false;
					break;
				}

			}
		}
	}

	private String insert(int position, String input, String strInsert) {
		StringBuilder builder = new StringBuilder(input);
		builder = builder.insert(position, strInsert);
		return builder.toString();
	}

	public String delete(String input, String delete) throws RemoteException {
		StringBuilder builder = new StringBuilder(input);
		int index = input.indexOf(delete);
		int lengt = delete.length();
		builder = builder.delete(index, index + lengt);
		return builder.toString();
	}

	private void sendToClient(Message command) throws IOException {
		if (socket.isConnected()) {
			objectOutputStream.writeObject(command);
		}
	}

	private Message receiveFromClient() throws ClassNotFoundException {
		try {
			if (socket.isConnected()) {
				Message command = (Message) objectInputStream.readObject();
				return command;
			}
		} catch (IOException e) {
		}
		return null;
	}

	@Override
	public void run() {
		try {
			handle();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.run();
	}
}
